import ComponentBase from "../engine/ComponentBase";
import { EventManager } from "../engine/EventManager";
import UIManager from "../engine/UIManager";
import { EventConfig } from "../game/config/EventConfig";
import { ViewConfig } from "../game/config/ViewConfig";
import { UserModel } from "../game/model/UserModel";
import { UIHelper } from "../game/util/UIHelper";
import HeadPrefab from "./HeadPrefab";


const { ccclass, property } = cc._decorator;

@ccclass
export default class TopUI extends ComponentBase {

    private lbName: cc.Label
    private lbId: cc.Label
    private lbDiamond: cc.Label
    private txtMeili: cc.Label
    private head: HeadPrefab

    onEnable(): void {
        super.onEnable()
        EventManager.instance.on(EventConfig.Event_Update_UserInfo, this.refresh, this)
        this.lbName = this.getChild(this.node, "lbName", cc.Label)
        this.lbId = this.getChild(this.node, "lbId", cc.Label)
        this.lbDiamond = this.getChild(this.node, "lbDiamond", cc.Label)
        this.head = this.getChild(this.node, "head", HeadPrefab);
        if(this.head){
            UIHelper.onAddEvent(this.head.node, () => {
                // UIManager.instance.showView(ViewConfig.viewConfig.ViewUserInfo)
            })
        }
        // let btnAddGems: cc.Node = this.findChild(youbiandi, "btnAddGems")
        // UIHelper.onAddEvent(btnAddGems, () => {
        //     // UIManager.instance.showView(ViewConfig.viewConfig.ViewShop)
        // })
        // let btnAddJinDou: cc.Node = this.findChild(youbiandi, "btnAddJinDou")
        // UIHelper.onAddEvent(btnAddJinDou, () => {
        //     // UIManager.instance.showView(ViewConfig.viewConfig.ViewShop)
        // })
        // let btnAddMeili: cc.Node = this.findChild(this.node, "btnAddMeili")
        // UIHelper.onAddEvent(btnAddMeili, () => {
        //     UIManager.instance.showView(ViewConfig.viewConfig.ViewMeilis)
        // })
        this.refresh()
    }

    private refresh() {
        if (this.lbId) {
            this.lbId.string = "ID:" + UserModel.instance.userId + ""
        }
        if (this.lbName) {
            this.lbName.string = UserModel.instance.userName
        }
        if (this.lbDiamond) {
            this.lbDiamond.string = UserModel.instance.diamond + ""
        }
        if (this.head) {
            this.head.setHead(UserModel.instance.userPhoto)
        }
    }


    onDisable(): void {
        super.onDisable()
    }
}
